package com.mygdx.game;

import com.badlogic.gdx.*;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.maps.MapLayer;
import com.badlogic.gdx.maps.MapLayers;
import com.badlogic.gdx.maps.MapObject;
import com.badlogic.gdx.maps.MapProperties;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapTileSet;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.scenes.scene2d.Event;
import com.badlogic.gdx.scenes.scene2d.EventListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.actions.AddAction;
import com.badlogic.gdx.scenes.scene2d.actions.MoveToAction;
import com.badlogic.gdx.scenes.scene2d.actions.SequenceAction;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Window;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;

import java.util.Iterator;

/**
 * Created by dell on 2018/2/6.
 * 使用tilemap创建地图
 */
public class MapGame extends ApplicationAdapter {

    OrthographicCamera camera;
    SpriteBatch batch;
    TiledMap map;
    OrthogonalTiledMapRenderer otrender;
    private Window window;

    private MyActor actor;

    ImageButton btn_ok;
    ImageButton btn_cancel;

    Stage stage;
    private Texture perTexture;
    private TextureRegion[] perRegions; //人物走路的每一帧

    private int curIndex = 0;
    @Override
    public void create() {
        super.create();

        Gdx.input.setInputProcessor(new InputAdapter(){
            @Override
            public boolean touchUp(int screenX, int screenY, int pointer, int button) {
                Gdx.app.error("tag","抬起了手指");
                return super.touchUp(screenX, screenY, pointer, button);
            }


            @Override
            public boolean touchDragged(int screenX, int screenY, int pointer) {
                if (screenX>=actor.getX())
                {
                    Gdx.app.error("tag","往前走 screenX = "+screenX);
                    if (curIndex>=perRegions.length-1)
                        curIndex = 0;
                    actor.setScaleX(1);
                    actor.setRegion(perRegions[++curIndex]);
                    actor.setPosition(screenX,actor.getY());
                }else if (screenX<actor.getX()){
                    Gdx.app.error("tag","往回走 screenX = "+screenX);
                    if (curIndex>=perRegions.length-1)
                        curIndex = 0;
                    actor.setScaleX(-1);
                    actor.setRegion(perRegions[++curIndex]);
                    actor.setPosition(screenX,actor.getY());
                }

                return super.touchDragged(screenX, screenY, pointer);
            }
        });
        batch = new SpriteBatch();
        stage = new Stage();
        setPerson();
        setMap();
        //setWindow();

        setButton();


    }

    private void setPerson() {
        perTexture = new Texture(Gdx.files.internal("animation_sheet.png"));
        int rows = 5;
        int cols = 6;
        int ceilWidth = perTexture.getWidth() / cols;
        int ceilHeight = perTexture.getHeight() / rows;
        TextureRegion[][] split = TextureRegion.split(perTexture, ceilWidth, ceilHeight);
        perRegions = new TextureRegion[rows * cols];
        int index = 0;
        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                perRegions[index++] = split[row][col];
            }
        }
        actor = new MyActor(perRegions[curIndex]);
        stage.addActor(actor);

    }

    private void setButton() {
    }

    private void setWindow() {
        TextureRegionDrawable textureRegionDrawable = new TextureRegionDrawable(new TextureRegion(new Texture("dialog.jpg")));
        BitmapFont bitmapFont = new BitmapFont(Gdx.files.internal("bitmapFont.fnt"));
        Window.WindowStyle windowStyle = new Window.WindowStyle(bitmapFont, Color.BLACK, textureRegionDrawable);
        window = new Window("hello world", windowStyle);
        window.setPosition(stage.getWidth() / 2 - window.getWidth() / 2, stage.getHeight() / 2 - window.getHeight() / 2);
    }

    private void setMap() {
        map = new TmxMapLoader().load("data/test.tmx");
        camera = new OrthographicCamera();
        otrender = new OrthogonalTiledMapRenderer(map); //渲染器
        camera.setToOrtho(false, 480, 320);   //设置视角
        MapLayers layers = map.getLayers();

        for (MapLayer layer : layers) {
            for (MapObject mapObject : layer.getObjects()) {
                Gdx.app.error("tag", "name = " + mapObject.getName());
                MapProperties properties = mapObject.getProperties();
                Iterator<String> keys = properties.getKeys();
                while (keys.hasNext()) {
                    String next = keys.next();
                    Gdx.app.error("tag", "key = " + next + " value = " + properties.get(next));
                }
                if ("matro".equals(mapObject.getName())) {
                    Float x = (Float) properties.get("x"); //获得的map坐标属性，会自动转成舞台使用的坐标大小
                    Float y = (Float) properties.get("y");
                    actor.setPosition(x, y);
                }
            }
        }
    }

    @Override
    public void render() {
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        //实现相机的变化进行渲染
        camera.update();
        otrender.setView(camera);
        otrender.render();
        stage.act();
        stage.draw();
        handleInput();
    }

    private float preY = 0;
    private Action curAction;
    private void handleInput() {
        if (Gdx.app.getType()!= Application.ApplicationType.Desktop)
        {
            return;
        }
        if (Gdx.input.isKeyJustPressed(Input.Keys.SPACE)) //点击了空格
        {
            for (Action action : actor.getActions()) {
                if (curAction.equals(action))
                {
                    return;
                }
            }

            preY = actor.getY();
            MoveToAction moveToAction1 = Actions.moveTo(actor.getX(), preY + 110, 1f);
            MoveToAction moveToAction2 = Actions.moveTo(actor.getX(), preY, 1f);
            SequenceAction sequence = Actions.sequence(moveToAction1, moveToAction2);
            curAction = sequence;
            actor.addAction(curAction);

        }
    }


    @Override
    public void dispose() {
        super.dispose();
        map.dispose();
        perTexture.dispose();
        batch.dispose();
    }
}
